<?php

namespace App\Http\Controllers;

use App\Http\Model\Article;
use App\Http\Model\Registration;
use App\Repositories\RegistrationRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class RegistrationController extends Controller
{

    protected $model;

    public function __construct(RegistrationRepository $registration)
    {
        $this->model = $registration;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $article_id = $request->input('article_id');

        $nums = $this->model->getAllByCondition('article_id', $article_id);

        return view('admin.registration.detail', ['nums' => $nums]);
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     *
     * registration/create  Get
     */
    public function create()
    {
        $announce = Article::where('category', '2')->where('status', '1')->get(['id', 'title']);

        return view('index.personal', ['announce' => $announce]);
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     *
     * registration  POST
     */
    public function store(Request $request)
    {
        $input=Input::all();
        $validator = Validator::make($request->input(), [
            'name' => 'required',
            'mobile' => 'required'
        ], [
            'name.required' => '姓名不能为空',
            'mobile.required' => '电话不能为空',
        ]);

        //判断是否有选公告
        if($request->input('article_id')=='-1'){
            return ['status_code'=>500,'msg'=>'请选择要报名的文章'];
        }

        if ($validator->passes()) {
            $result=Registration::where('mobile',$request->input('mobile'))->where('article_id',$request->input('article_id'))->get();

            //判断是否已经报名
            if($result->isEmpty()){
                $this->model->store($input);
                return ['status_code'=>200];
            }else{
                return ['status_code'=>500,'msg'=>'同一活动不能多次报名喔'];
            }
        } else {
            return ['status_code' => 500, 'msg' => $validator->errors()->first()];
        }

        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


    /**
     * 公告列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function article(Request $request)
    {
        if(!empty($request->input('name'))){
            $articles=Article::where('title','like','%'.$request->input('name').'%')->where('category','2')->orderByDesc('created_at')->paginate(config('constants.PAGE_SIZE'));
        }else{
            $articles=Article::where('category','2')->orderByDesc('created_at')->paginate(config('constants.PAGE_SIZE'));

        }
        return view('admin.registration.index',['articles'=>$articles]);
    }
}
